#include "com_llsydn_ndkdemo_JavaCJNI.h"
#include <string.h>
#include <android/log.h>
#define LOG_TAG "System.out"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
/**
 *
 * java语言调用c语言
 *
 */


/**
 * 把一个jstring转换成一个c语言的char* 类型.
 */
char* _JString2CStr(JNIEnv* env, jstring jstr) {
    char* rtn = NULL;
    jclass clsstring = (*env)->FindClass(env, "java/lang/String");
    jstring strencode = (*env)->NewStringUTF(env,"GB2312");
    jmethodID mid = (*env)->GetMethodID(env, clsstring, "getBytes", "(Ljava/lang/String;)[B");
    jbyteArray barr = (jbyteArray)(*env)->CallObjectMethod(env, jstr, mid, strencode); // String .getByte("GB2312");
    jsize alen = (*env)->GetArrayLength(env, barr);
    jbyte* ba = (*env)->GetByteArrayElements(env, barr, JNI_FALSE);
    if(alen > 0) {
        rtn = (char*)malloc(alen+1); //"\0"
        memcpy(rtn, ba, alen);
        rtn[alen]=0;
    }
    (*env)->ReleaseByteArrayElements(env, barr, ba,0);
    return rtn;
}

/**
* jstring：返回类型
* Java_全类名_方法名
* JNIEnv* env：里面有很多方法
* jobject jobj：谁调用了这个方法就是谁的实例
*/
JNIEXPORT jstring JNICALL Java_com_llsydn_ndkdemo_JavaCJNI_sayHello(JNIEnv* env,jobject jobj){
    char* text="I am form c";
    return (*env)->NewStringUTF(env, text);
};

/*
 * 加法运算
 * Class:     com_llsydn_ndkdemo_JNI
 * Method:    add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_llsydn_ndkdemo_JavaCJNI_add(JNIEnv* env, jobject jobj, jint ji, jint jj){
    int result = ji+jj;
    LOGE("value===%d\n",result);
    return result;
};


/*
 * 拼接字符串
 * Class:     com_llsydn_ndkdemo_JNI
 * Method:    lianJie
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_llsydn_ndkdemo_JavaCJNI_lianJie(JNIEnv* env, jobject jobj, jstring js){
    //需要将string转char
    char* fromJava = _JString2CStr(env, js);
    char* fromC = " and I am from c";
    //拼接，会把拼接的结果放在第一个参数
    strcat(fromJava, fromC);
    //返回，需要将char转string
    return  (*env)->NewStringUTF(env,fromJava);
};

/*
 * 给数组每个元素加10
 * Class:     com_llsydn_ndkdemo_JNI
 * Method:    increaseArrayEles
 * Signature: ([I)[I
 */
JNIEXPORT jintArray JNICALL Java_com_llsydn_ndkdemo_JavaCJNI_increaseArrayEles(JNIEnv* env, jobject jobj, jintArray jarray){
    //1.得到数组的长度
    //jsize(*GetArrayLength)(JNIEnv*, jarray);
    jsize size = (*env)->GetArrayLength(env,jarray);
    //2.得到数组元素
    //jint*(*GetIntArrayElements)(JNIEnv*, jintArray, jboolean*);
    jint* intArray = (*env)->GetIntArrayElements(env,jarray,JNI_FALSE);
    //3.遍历数组，给每个元素加上10
    int i;
    for(i =0;i<size;i++){
        //*(intArray+i) = *(intArray+i) + 10;
        *(intArray+i) +=  10;
    }
    //4.返回结果
    return  jarray;
};


/*
 * 校验密码是否正确
 * Class:     com_llsydn_ndkdemo_JNI
 * Method:    checkPwd
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_llsydn_ndkdemo_JavaCJNI_checkPwd(JNIEnv* env, jobject jobj, jstring jstr){
    //服务器的密码是123456
    char* origin = "123456";
    char* fromUser = _JString2CStr(env,jstr);
    //函数比较字符串是否相同
    int code = strcmp(origin,fromUser);
    if(code==0){
        return 200;
    }else{
        return 400;
    }
};
